/*
 * File:   main.cpp
 * Author: ScaV
 *
 * Created on 15 РЎРµРЅС‚СЏР±СЂСЊ 2011 Рі., 17:46
 */

#include "infint.h"
#include "calculator.h"
#include <fstream>
#include <string>

using namespace std;

#include <ctime>
#include <cstdlib>
void randomTest();
/*
 *
 */
int main(int argc, char** argv) {
try {
	//randomTest();
	calculator clc;
	ifstream in;
	ofstream out;

	if (argc < 2)
		return 0;
	in.open(argv[1], ios::in);
	if (argc == 3)
		out.open(argv[2], ios::out);
	if (argc == 2 || !out.is_open())
		out.open("CON", ios::out);
	if (!in.is_open()) {
		cout << "Can't open file!" << endl;
		return 0;
	}
	string str;
	char ch;
	while (1) {
		ch = in.get();
		if (ch == '\n' || in.eof()) {
			if (!str.empty()) {
				out << clc(str.c_str()) << endl;
				str.clear();
			}
			if (in.eof())
				break;
			continue;
		}
		str += ch;
	}
} catch (const char* str) {
		cout << str << endl;
}
	return 0;
}

void randomTest() {
	int choise;
	long a, b;
	srand(time(NULL));
	for (infint i = 0; ; ++i) {
		choise = (rand() % 4);
		a = rand() * ((rand() % 2)?(1):(-1));
		b = rand() * ((rand() % 2)?(1):(-1));
		infint a1(a), b1(b), c;
		choise = 3;
		switch (choise) {
			case 0:
				cout << "\r" << a1 << "+" << b1;
				c = a1 + b1;
				if (c != a + b)
					cout << "\rERR: " << a1 << " + " << b1 << " != " << c << " (" << a << " + " << b << " = " << a + b << ")" << endl;	break;
				break;
			case 1:
				cout << "\r" << a1 << "-" << b1;
				c = a1 - b1;
				if (c != a - b)
					cout << "\rERR: " << a1 << " - " << b1 << " != " << c << " (" << a << " - " << b << " = " << a - b << ")" << endl;	break;
			case 2:
				cout << "\r" << a1 << "*" << b1;
				c = a1 * b1;
				if (c != a * b)
					cout << "\rERR: " << a1 << " * " << b1 << " != " << c << " (" << a << " * " << b << " = " << a * b << ")" << endl;	break;
			case 3:
				if (b1 == 0)
					break;
				cout << "\r" << a1 << "/" << b1;
				c = a1 / b1;
				if (c != a / b)
					cout << "\rERR: " << a1 << " / " << b1 << " != " << c << " (" << a << " / " << b << " = " << a / b << ")" << endl;	break;
			default:
				throw "Exception in rand!";
		}
		cout << "\r" << i << " " << choise << "\t\t";
	}
}
